package dasregistry.ontology.browsing;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Set;

import org.biojava.ontology.OntoTools;
import org.biojava.ontology.Ontology;
import org.biojava.ontology.OntologyException;
import org.biojava.ontology.OntologyFactory;
import org.biojava.ontology.Term;
import org.biojava.ontology.io.OboParser;

public class OntologyReader {
	
	public static void main(String [] args){
		OntologyReader reader=new OntologyReader();
		try {
			Ontology ont=reader.readOntology("SO", "sequence ontology", "so.obo");
			
		} catch (OntologyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private Ontology readOntology(String ontoName, String ontoDesc,
			String fileName) throws OntologyException {

		OboParser parser = new OboParser();
		InputStream inStream = this.getClass().getResourceAsStream(
				"/ontologies/" + fileName);
		System.out.println("reading ontology: /ontologies/" + fileName);
		if (inStream == null) {
			System.err.println("did not find " + fileName);
		}

		BufferedReader oboFile = new BufferedReader(new InputStreamReader(
				inStream));

		try {
			Ontology ontology = parser.parseOBO(oboFile, ontoName, ontoDesc);

			 System.out.println("finished parsing: " + ontology);
			 Set keys = ontology.getTerms();
			 Iterator<Term> iter = keys.iterator();
			 while (iter.hasNext()){
			 Term term = iter.next();
			 System.out.println(term + " " + term.getDescription());
			 

			 }
			return ontology;

		} catch (Exception e) {
			e.printStackTrace();
		}

		// return a dummy ontology
		OntologyFactory factory = OntoTools.getDefaultFactory();
		Ontology ontology = factory.createOntology(ontoName, ontoDesc);

		return ontology;

	}
}
